package com.hz.monitoring.module.hardware.controller.admin.sensor_tenant_resource;

import com.hz.monitoring.framework.common.pojo.CommonResult;
import com.hz.monitoring.framework.common.pojo.PageResult;
import com.hz.monitoring.framework.excel.core.util.ExcelUtils;
import com.hz.monitoring.framework.operatelog.core.annotations.OperateLog;
import com.hz.monitoring.module.hardware.controller.admin.sensor_tenant_resource.vo.*;
import com.hz.monitoring.module.hardware.convert.sensor_tenant_resource.SensorTenantResourceConvert;
import com.hz.monitoring.module.hardware.dal.dataobject.sensor_tenant_resource.SensorTenantResourceDO;
import com.hz.monitoring.module.hardware.service.sensor_tenant_resource.SensorTenantResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.hz.monitoring.framework.common.pojo.CommonResult.success;
import static com.hz.monitoring.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 传感器租户配置")
@RestController
@RequestMapping("/hardware/sensor-tenant-resource")
@Validated
public class SensorTenantResourceController {

    @Resource
    private SensorTenantResourceService sensorTenantResourceService;

    @PostMapping("/create")
    @ApiOperation("创建传感器租户配置")
    @PreAuthorize("@ss.hasPermission('hardware:sensor-tenant-resource:create')")
    public CommonResult<Long> createSensorTenantResource(@Valid @RequestBody SensorTenantResourceCreateReqVO createReqVO) {
        return success(sensorTenantResourceService.createSensorTenantResource(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新传感器租户配置")
    @PreAuthorize("@ss.hasPermission('hardware:sensor-tenant-resource:update')")
    public CommonResult<Boolean> updateSensorTenantResource(@Valid @RequestBody SensorTenantResourceUpdateReqVO updateReqVO) {
        sensorTenantResourceService.updateSensorTenantResource(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除传感器租户配置")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('hardware:sensor-tenant-resource:delete')")
    public CommonResult<Boolean> deleteSensorTenantResource(@RequestParam("id") Long id) {
        sensorTenantResourceService.deleteSensorTenantResource(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得传感器租户配置")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('hardware:sensor-tenant-resource:query')")
    public CommonResult<SensorTenantResourceRespVO> getSensorTenantResource(@RequestParam("id") Long id) {
        SensorTenantResourceDO sensorTenantResource = sensorTenantResourceService.getSensorTenantResource(id);
        return success(SensorTenantResourceConvert.convert(sensorTenantResource));
    }

    @GetMapping("/list")
    @ApiOperation("获得传感器租户配置列表")
    @PreAuthorize("@ss.hasPermission('hardware:sensor-tenant-resource:query')")
    public CommonResult<List<SensorTenantResourceRespVO>> getSensorTenantResourceList(SensorTenantResourceListReqVO listReqVO) {
        List<SensorTenantResourceDO> list = sensorTenantResourceService.getSensorTenantResourceList(listReqVO);
        return success(SensorTenantResourceConvert.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得传感器租户配置分页")
    @PreAuthorize("@ss.hasPermission('hardware:sensor-tenant-resource:query')")
    public CommonResult<PageResult<SensorTenantResourceRespVO>> getSensorTenantResourcePage(@Valid SensorTenantResourcePageReqVO pageVO) {
        PageResult<SensorTenantResourceDO> pageResult = sensorTenantResourceService.getSensorTenantResourcePage(pageVO);
        return success(SensorTenantResourceConvert.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出传感器租户配置 Excel")
    @PreAuthorize("@ss.hasPermission('hardware:sensor-tenant-resource:export')")
    @OperateLog(type = EXPORT)
    public void exportSensorTenantResourceExcel(@Valid SensorTenantResourceExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<SensorTenantResourceDO> list = sensorTenantResourceService.getSensorTenantResourceList(exportReqVO);
        // 导出 Excel
        List<SensorTenantResourceExcelVO> datas = SensorTenantResourceConvert.convertList02(list);
        ExcelUtils.write(response, "传感器租户配置.xls", "数据", SensorTenantResourceExcelVO.class, datas);
    }

}
